{% extends "base.html" %}

{% block title %}v2ray管理器{% endblock %}

{% block heard %}v2ray管理器{% endblock %}

{% block content %}
  <!--  常用按钮  -->
    <div class="layui-row">
        <button type="button" class="layui-btn layui-btn-lg layui-btn-primary layui-btn-radius" lay-event="synchronous">
          同步 <i class="layui-icon layui-icon-refresh-1"></i>
        </button>
        <button type="button" class="layui-btn layui-btn-lg layui-btn-primary layui-btn-radius" lay-event="add">
          添加 <i class="layui-icon layui-icon-add-circle-fine"></i>
        </button>
    </div>
    <!-- 内容信息  -->
    <div class="layui-bg-gray layui-bg-black" style="padding: 16px;">
      <div class="layui-row layui-col-space15">
          {% for item in data %}
              <div class="layui-col-md6">
                <div class="layui-bg-black user_url_check" data-target="{{ item.url }}">
                    <fieldset class="layui-elem-field">
                      <legend>{{ item.name }}</legend>
                      <div class="layui-field-box" style="word-wrap: break-word; word-break: break-all;">
                        {{ item.user_id }}
                      </div>
                    </fieldset>
                </div>
              </div>
          {% endfor %}
      </div>
    </div>
{% endblock %}

{% block js_content %}
    <script>
        layui.define(function(exports){
            // 输出接口
            exports('common', {
                run_start: function () {
                    // 粘贴用户信息
                    layui.$('.user_url_check').on("click", function (){
                            var url = layui.$(this).attr('data-target');
                            layer.open({
                                type: 1,
                                title: '协议地址',
                                shadeClose: true,
                                shade: 0.8,
                                area: ['90%', '50%'],
                                content: `<div style="word-wrap: break-word; word-break: break-all;margin: 5%;">` + url + `</div>`,
                                btn: ['复制'],
                                btnAlign: 'c',
                                yes: function(index, layero){
                                    // 赋值给粘贴板
                                    navigator.clipboard.writeText(url).then(() => {
                                        layer.msg("复制成功");
                                        layer.closeAll();
                                      },() => {
                                        layer.msg("复制失败");
                                      });
                                }
                            });
                      });
                    // 同步用户
                    function synchronous()
                    {
                        $.ajax({
                            url: '/v2ray/synchronous',
                            type: 'POST',
                            dataType: 'json',
                            success: function(data) {
                                if (data.code === 0) {
                                    layer.msg('同步成功');
                                    location.href = "/v2ray/";
                                } else {
                                    layer.msg('同步失败');
                                }
                                layer.closeAll()
                            }
                        });
                    }

                    // 添加文件
                    function add()
                    {
                            let html = `
                                <div class="layui-form-item">
                                    <div class="layui-input-group">
                                      <div class="layui-input-split layui-input-prefix">
                                        用户名
                                      </div>
                                      <input type="text" placeholder="输入用户名" id="name" class="layui-input">
                                    </div>
                                  </div>
                            `;
                            layer.open({
                                type: 1,
                                title: '添加用户',
                                shadeClose: true,
                                shade: 0.8,
                                area: ['400px', '300px'],
                                content: html,
                                btn: ['确定'],
                                yes: function(index, layero){
                                    let input = top.$('#name').val();
                                    $.ajax({
                                        url: '/v2ray/add',
                                        type: 'POST',
                                        data: JSON.stringify({"name": input}),
                                        dataType: 'json',
                                        success: function(response) {
                                            layer.msg(response.data);
                                            location.href = "/v2ray/";
                                        }
                                    });
                                }
                            });
                        }
                }
            });
        });
    </script>
{% endblock %}